home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / ghostscr / gs252ini.zip / IMPATH.PS < prev    next >
Text File  |  1992-08-20  |  6KB  |  183 lines

  1. %    Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2. %    Distributed by Free Software Foundation, Inc.
  3. %
  4. % This file is part of Ghostscript.
  5. %
  6. % Ghostscript is distributed in the hope that it will be useful, but
  7. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. % to anyone for the consequences of using it or for whether it serves any
  9. % particular purpose or works at all, unless he says so in writing.  Refer
  10. % to the Ghostscript General Public License for full details.
  11. %
  12. % Everyone is granted permission to copy, modify and redistribute
  13. % Ghostscript, but only under the conditions described in the Ghostscript
  14. % General Public License.  A copy of this license is supposed to have been
  15. % given to you along with Ghostscript so you can know your rights and
  16. % responsibilities.  It should be in a file named COPYING.  Among other
  17. % things, the copyright notice and this notice must be preserved on all
  18. % copies.
  19.  
  20. % impath.ps
  21. % Reverse-rasterize a bitmap to produce a Type 1 outline.
  22. % (This was formerly a Ghostscript operator implemented in C.)
  23.  
  24. %    <image> <width> <height> <wx> <wy> <ox> <oy> <string>
  25. %      type1imagepath <substring>
  26. %        Converts an image (bitmap) description of a character into
  27. %          a scalable description in Adobe Type 1 format.  The
  28. %          current transformation matrix should be the same as the
  29. %          FontMatrix of the font in which this character will be
  30. %          used: this establishes the scaling relationship between
  31. %          image pixels (the image is assumed to be 1 unit high in
  32. %          user space) and the character coordinate system used in
  33. %          the scalable description.  wx and wy are the character
  34. %          width, and ox and oy are the character origin relative
  35. %          to the lower left corner of the bitmap, in *pixels*.
  36. %        The image is assumed to be stored in left-to-right,
  37. %          top-to-bottom order.  Note that this is not consistent
  38. %          with the `image' operator's interpretation of the CTM.
  39. %        All coordinates in the scalable description are rounded to
  40. %          integers, so the coefficients in the FontMatrix should
  41. %          be on the order of 1/N for some value of N that is
  42. %          either a multiple of the height/width or is large
  43. %          compared to the width and height.  (There is a
  44. %          convention, which some PostScript programs rely on, that
  45. %          N=1000.)
  46. %        Note that the encoded description has *not* been subjected
  47. %          to CharString encryption, which is necessary before the
  48. %          description can be given to type1addpath: to do this,
  49. %          follow the type1imagepath with
  50. %            4330 exch dup type1encrypt exch pop
  51. %        If the description is too complex to fit into the supplied
  52. %          string, a limitcheck error results.  A good rule of
  53. %          thumb is that the size of the string should be about 6
  54. %          times the number of 1-bits in the image that are not
  55. %          completely surrounded by other 1-bits.
  56.  
  57. % Import the Type 1 opcodes.
  58. (type1ops.ps) run
  59.  
  60. 100 dict
  61. dup /type1imagepath_dict exch def
  62. begin
  63.  
  64. /rc { round cvi } bind def
  65. /moving [/rmoveto /hmoveto /vmoveto] def
  66. /drawing [/rlineto /hlineto /vlineto] def
  67.  
  68. % Convert the current path to a Type 1 token array.
  69. /putxy            % x y ops -> cs_elements
  70.  { 3 -1 roll dup x sub rc exch /x exch def
  71.    3 -1 roll dup y sub rc exch /y exch def
  72.    % stack: ops dx dy
  73.    dup 0 eq
  74.     { % dy = 0, use hmoveto/lineto
  75.       pop exch 1 get
  76.     }
  77.     { 1 index 0 eq
  78.        { % dx = 0, use vmoveto/lineto
  79.          exch pop exch 2 get
  80.        }
  81.        { % use rmoveto/rlineto
  82.          3 -1 roll 0 get
  83.        }
  84.       ifelse
  85.     }
  86.    ifelse
  87.  } bind def
  88. /pathtotype1        % -> charstack
  89.  { 3 dict begin /x 0 def /y 0 def
  90.    mark
  91.    { moving putxy
  92.    }
  93.    { drawing putxy
  94.    }
  95.    { % Convert curve to relative form
  96.      x y 3
  97.       { exch neg 7 index add rc
  98.         exch neg 6 index add rc
  99.     8 -2 roll
  100.       }
  101.      repeat /y exch def /x exch def
  102.      1 index 0 eq 5 index 0 eq and    % dy1=dx3=0, hv
  103.       { 5 -1 roll pop exch pop /hvcurveto
  104.       }
  105.       { dup 0 eq 6 index 0 eq and    % dx1=dy3=0, vh
  106.          { 6 -1 roll pop pop /vhcurveto
  107.      }
  108.      { /rrcurveto            % none of the above
  109.      }
  110.     ifelse
  111.       }
  112.      ifelse
  113.    }
  114.    { /closepath
  115.    }
  116.    pathforall end
  117.  } bind def
  118.  
  119. end    % type1imagepath_dict
  120.  
  121. % The main program
  122. /type1imagepath        % image width height wx wy ox oy string ->
  123.             % substring
  124.  { type1imagepath_dict begin
  125.    /tsave save def
  126.    /ostring exch def
  127.    /oy exch def   /ox exch def
  128.    /wy exch def   /wx exch def
  129.    /height exch def   /width exch def
  130.    /data exch def
  131.  
  132.    /ofilter ostring /NullEncode filter def
  133.    /raster width 7 add 8 idiv def
  134.  
  135. % Construct the coordinate transformation.
  136.    height dup scale
  137.      matrix currentmatrix matrix invertmatrix setmatrix
  138.  
  139. % Determine the left side bearing.
  140.    /lsbx width
  141.    0 1 width 1 sub
  142.     { dup dup 8 idiv 0 exch
  143.       raster raster height mul 1 sub
  144.        { data exch get or }
  145.       for exch 8 mod bitshift 128 and 0 ne
  146.        { exch pop exit }
  147.       if pop
  148.     }
  149.    for def
  150.  
  151. % Compute and encode the origin, width, and side bearing.
  152.    mark
  153.    ox oy dtransform
  154.      rc /opty exch def   rc /optx exch def
  155.    wx wy dtransform
  156.      rc /iwy exch def   rc /iwx exch def
  157.    lsbx ox sub 0 dtransform
  158.      rc /ilsby exch def   rc /ilsbx exch def
  159.    ilsbx
  160.    iwy 0 ne ilsby 0 ne or
  161.     { ilsby iwx iwy /sbw }
  162.     { iwx /hsbw }
  163.    ifelse
  164.    ofilter charstack_write
  165.  
  166. % Flip over the Y axis, because the image is stored top-to-bottom.
  167.    [1 0 0 -1 0 height] concat
  168. % Account for the character origin.
  169.    ox lsbx sub oy translate
  170. % Trace the outline.
  171.    newpath
  172.    width height data .imagepath
  173.    gsave matrix setmatrix pathtotype1 grestore
  174.    ofilter charstack_write
  175.  
  176. % Terminate the output
  177.    ofilter mark /endchar charstack_write
  178.  
  179.    ostring 0 ofilter fileposition getinterval
  180.    tsave restore
  181.    end
  182.  } bind def
  183.